// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MarketQuoteList.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge
{
    #region Usings

    using System;
    using System.Collections.Generic;

    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The quote list.
    /// </summary>
    [Serializable]
    public class MarketQuoteList
    {
        #region Constants and Fields

        /// <summary>
        /// The price.
        /// </summary>
        private readonly double price;

        /// <summary>
        /// The mQuotelist.
        /// </summary>
        private readonly IDictionary<string, MarketQuote> quotelist;

        /// <summary>
        /// The locker.
        /// </summary>
        private Locker locker;

        /// <summary>
        /// The totalSize.
        /// </summary>
        private int totalSize;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MarketQuoteList"/> class.
        /// </summary>
        /// <param name="price">
        /// The price.
        /// </param>
        public MarketQuoteList(double price)
        {
            this.price = price;
            this.locker = new Locker();
            this.quotelist = new Dictionary<string, MarketQuote>();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets Contributions.
        /// </summary>
        public IContribution[] Contributions { get; set; }

        /// <summary>
        /// Gets Count.
        /// </summary>
        public int Count
        {
            get
            {
                return this.quotelist.Count;
            }
        }

        /// <summary>
        /// Gets or sets LastUpdated.
        /// </summary>
        public DateTime LastUpdated { get; set; }

        /// <summary>
        /// Gets Price.
        /// </summary>
        public double Price
        {
            get
            {
                return this.price;
            }
        }

        /// <summary>
        /// Gets TotalSize.
        /// </summary>
        public int TotalSize
        {
            get
            {
                return this.totalSize;
            }
        }

        #endregion

        #region Indexers

        /// <summary>
        /// The this.
        /// </summary>
        /// <param name="quoteKey">
        /// The quoteKey.
        /// </param>
        public MarketQuote this[string quoteKey]
        {
            get
            {
                return this.quotelist[quoteKey];
            }

            private set
            {
                this.quotelist[quoteKey] = value;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The add.
        /// </summary>
        /// <param name="marketQuoteUpdate">
        /// The marketQuoteUpdate.
        /// </param>
        public void Add(MarketQuote marketQuoteUpdate)
        {
            using (this.locker.Lock())
            {
                // Stdout.WriteLine("MarketQuoteList(): {0} + ID={1} Size={2} Contributor={3}", PrettyPrinter.Prices.Format(price, 0.000001), marketQuoteUpdate.ID, marketQuoteUpdate.Size, marketQuoteUpdate.Contributor);

                // Quote added
                this.quotelist.Add(marketQuoteUpdate.QuoteKey, marketQuoteUpdate);

                // Update
                this.UpdateContributions();
            }

            // Stdout.Write(" Added to book " + PrettyPrinter.Timings.Format(Timings.Now));
        }

        /// <summary>
        /// The contains.
        /// </summary>
        /// <param name="quoteKey">
        /// The quoteKey.
        /// </param>
        /// <returns>
        /// The contains.
        /// </returns>
        public bool Contains(string quoteKey)
        {
            return this.quotelist.ContainsKey(quoteKey);
        }

        /// <summary>
        /// The remove.
        /// </summary>
        /// <param name="quoteKey">
        /// The quoteKey.
        /// </param>
        /// <returns>
        /// The remove.
        /// </returns>
        public bool Remove(string quoteKey)
        {
            using (this.locker.Lock())
            {
                // Stdout.WriteLine("MarketQuoteList(): {0} - ID={1} Size={2} Contributor={3}", PrettyPrinter.Prices.Format(price, 0.000001), quote.ID, quote.Size, quote.Contributor);

                // Remove
                var b = this.quotelist.Remove(quoteKey);

                // Update
                this.UpdateContributions();

                return b;
            }

            // Stdout.Write(" Removed from book " + PrettyPrinter.Timings.Format(Timings.Now));
        }

        /// <summary>
        /// The update.
        /// </summary>
        /// <param name="marketQuoteUpdate">
        /// The marketQuoteUpdate.
        /// </param>
        public void Update(MarketQuote marketQuoteUpdate)
        {
            using (this.locker.Lock())
            {
                // Stdout.WriteLine("MarketQuoteList(): {0} u ID={1} Size={2} Contributor={3}", PrettyPrinter.Prices.Format(price, 0.000001), marketQuoteUpdate.ID, marketQuoteUpdate.Size, marketQuoteUpdate.Contributor);
                var currentQuote = this[marketQuoteUpdate.QuoteKey];
                this[marketQuoteUpdate.QuoteKey] = MarketQuote.Quote(
                    currentQuote.QuoteKey, marketQuoteUpdate.Stamp, currentQuote.Side, currentQuote.Price, marketQuoteUpdate.Size, currentQuote.Contribution);

                // Update
                this.UpdateContributions();
            }

            // Stdout.Write(" Updated in book " + PrettyPrinter.Timings.Format(Timings.Now));
        }

        #endregion

        #region Methods

        /// <summary>
        /// The update contributions.
        /// </summary>
        private void UpdateContributions()
        {
            // The size is now that one
            this.totalSize = 0;

            this.Contributions = new IContribution[this.quotelist.Keys.Count];
            var n = 0;
            foreach (var q in this.quotelist.Values)
            {
                this.Contributions[n++] = q.Contribution;
                this.totalSize += q.Size;
            }

            this.LastUpdated = Timings.Now;
        }

        #endregion
    }
}